﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json;

namespace SVN.SourceCode.BackUp
{
    public partial class FileBackupUI : Form
    {
        FileBackupLogic fileBackup = null;

        public FileBackupUI()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = txtSourceFolder.Text;
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtSourceFolder.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        bool IsValid()
        {
            bool isValid = true;

            if (Directory.Exists(txtSourceFolder.Text) == false)
            {
                MessageBox.Show(this, "Source Folder Directory not found", "Error");
                txtSourceFolder.Focus();
                isValid = false;
            }

            return isValid;
        }

        private void btnTakeBackUp_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy == false)
            {
                if (IsValid() == false)
                    return;

                fileBackup = new FileBackupLogic(txtSourceFolder.Text, txtSourceFolder.Text,
                   new List<string>(txtFolderToSkip.Text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)),// { ".svn", "obj", "bin" },
                   new List<string>(txtExtensionToSkip.Text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))// { ".zip", ".msi", ".rar" });
                   );
                txtLog.Clear();


                var objRepo = new LocalScreenInfo();

                objRepo.sourcePath = txtSourceFolder.Text;
                objRepo.FolderToSkip = txtFolderToSkip.Text;
                objRepo.ExtensionToSkip = txtExtensionToSkip.Text;

                try
                {
                    File.WriteAllText("LocalScreenInfo.json", JsonConvert.SerializeObject(objRepo));
                }
                catch { }

                pnlInput.Enabled = false;
                backgroundWorker1.RunWorkerAsync();
                btnTakeBackUp.Text = "Cancel";
            }
            else
            {
                if (backgroundWorker1.CancellationPending)
                    return;

                if (MessageBox.Show(this, "Do you want cancel backUp", "Confirmation", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    backgroundWorker1.CancelAsync();
                }
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            fileBackup.ZipFile(backgroundWorker1);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            txtLog.AppendText(e.UserState + "\n");

            if (progressBar1.Value == 100)
            {
                MessageBox.Show(this, e.UserState + "\n", "Info");
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            pnlInput.Enabled = true;
            btnTakeBackUp.Text = "Take BackUp";
            progressBar1.Value = 0;
        }

        private void FileBackupUI_Load(object sender, EventArgs e)
        {
            if (File.Exists("LocalScreenInfo.json") == false)
                return;

            string temp = File.ReadAllText("LocalScreenInfo.json");

            var objRepo = JsonConvert.DeserializeObject<LocalScreenInfo>(temp);

            txtSourceFolder.Text = objRepo.sourcePath;
            txtFolderToSkip.Text = objRepo.FolderToSkip;
            txtExtensionToSkip.Text = objRepo.ExtensionToSkip;
        }
    }

    public class LocalScreenInfo
    {
        public string sourcePath { get; set; }
        public string FolderToSkip { get; set; }
        public string ExtensionToSkip { get; set; }
    }

}
